package com.mmloo.manage.service;

import com.alibaba.fastjson.JSONObject;
import com.mmloo.common.model.*;
import com.mmloo.common.repository.CURDRepository;
import com.mmloo.common.util.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by jessy on 2015/9/29.
 * 服务于商品的各种属性：品牌、产地、城市等等
 */
@Service
public class AttributesService {
    @Autowired
    CURDRepository curdRepository;

    /**
     * 生成id
     * @return
     * @throws Exception
     */
    public String  generateId(String tableName) throws Exception {
        ArrayList arrayList = new ArrayList();
        arrayList.add(tableName.getBytes());
        curdRepository.incr(arrayList);
        return curdRepository.get(arrayList);
    }

    public void addCategory(Category category) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.CATEGORY_DETAIL_DATA.getBytes());
        String id = generateId(DataTable.CATEGORY_ID_SQUENCE);
        hsetparames.add(id.getBytes());
        category.setId(id);
        hsetparames.add(category.toJson().getBytes());
        curdRepository.hset(hsetparames);
    }

    public void addCity(City city) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.CITY_DETAIL_DATA.getBytes());
        String id = generateId(DataTable.CITY_ID_SQUENCE);
        hsetparames.add(id.getBytes());
        city.setId(id);
        hsetparames.add(city.toJson().getBytes());
        curdRepository.hset(hsetparames);
    }

    public void addBrand(Brand brand) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.BRAND_DETAIL_DATA.getBytes());
        String id = generateId(DataTable.BRAND_ID_SQUENCE);
        hsetparames.add(id.getBytes());
        brand.setId(id);
        hsetparames.add(brand.toJson().getBytes());
        curdRepository.hset(hsetparames);
    }

    public void addOrigin(Origin origin) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.ORIGIN_DETAIL_DATA.getBytes());
        String id =generateId(DataTable.ORIGIN_ID_SQUENCE);
        hsetparames.add(id.getBytes());
        origin.setId(id);
        hsetparames.add(origin.toJson().getBytes());
        curdRepository.hset(hsetparames);
    }

    public void addAttribute(Attribute attribute) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.ATTRIBUTE_DETAIL_DATA.getBytes());
        String id = generateId(DataTable.ATTRIBUTE_ID_SQUENCE);
        hsetparames.add(id.getBytes());
        attribute.setId(id);
        hsetparames.add(attribute.toJson().getBytes());
        curdRepository.hset(hsetparames);
    }
    public Inventory addInventory(Inventory inventory) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.INVENTORY_DETAIL_dATA.getBytes());
        String id = generateId(DataTable.INVENTORY_ID_SQUENCE);
        hsetparames.add(id.getBytes());
        inventory.setId(id);
        hsetparames.add(inventory.toJson().getBytes());
        curdRepository.hset(hsetparames);
        return inventory;
    }


    public void addAttributeOptions(AttributeOption attributeOption) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.ATTRIBUTE_OPTIONS_DETAIL_DATA.getBytes());
        String id = /*generateId(DataTable.ATTRIBUTE_OPTIONS_ID_SQUENCE);*/attributeOption.getId();
        hsetparames.add(id.getBytes());
        attributeOption.setId(id);
        hsetparames.add(attributeOption.toJson().getBytes());
        curdRepository.hset(hsetparames);
    }

    /**
     * 查找所有的类别
     * @return
     * @throws Exception
     */
    public List<Category> findAllCategory() throws Exception {
        Map<String, String> categories = curdRepository.hscanAll(DataTable.CATEGORY_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = categories.entrySet();
        List<Category> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                Category category = JSONObject.parseObject(value, Category.class);
                list.add(category);
            }

        return list;
    }

    /**
     * 查找所有城市
     * @return
     * @throws Exception
     */
    public List<City> findAllCity() throws Exception {
        Map<String, String> cities = curdRepository.hscanAll(DataTable.CITY_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = cities.entrySet();
        List<City> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                City city = JSONObject.parseObject(value, City.class);
                list.add(city);
            }

        return list;
    }

    /**
     * 查找所有品牌
     * @return
     * @throws Exception
     */
    public List<Brand> findAllBrand() throws Exception {
        Map<String, String> brands = curdRepository.hscanAll(DataTable.BRAND_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = brands.entrySet();
        List<Brand> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                Brand brand = JSONObject.parseObject(value, Brand.class);
                list.add(brand);
            }

        return list;
    }

    /**
     * 查找所有产地
     * @return
     * @throws Exception
     */
    public List<Origin> findAllOrigin() throws Exception {
        Map<String, String> origins = curdRepository.hscanAll(DataTable.ORIGIN_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = origins.entrySet();
        List<Origin> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                Origin origin = JSONObject.parseObject(value, Origin.class);
                list.add(origin);
            }

        return list;
    }

    /**
     * 查找所有属性
     * @return
     * @throws Exception
     */
    public List<Attribute> findAllAttribute() throws Exception {
        Map<String, String> attributes = curdRepository.hscanAll(DataTable.ATTRIBUTE_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = attributes.entrySet();
        List<Attribute> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                Attribute attribute = JSONObject.parseObject(value, Attribute.class);
                list.add(attribute);
            }

        return list;
    }

    public List<AttributeOption> findAllAttrOption()throws Exception{
        Map<String, String> attributes = curdRepository.hscanAll(DataTable.ATTRIBUTE_OPTIONS_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = attributes.entrySet();
        List<AttributeOption> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                AttributeOption attribute = JSONObject.parseObject(value, AttributeOption.class);
                list.add(attribute);
            }

        return list;
    }

//    public void test() throws Exception {
//
//        List<AttributeOption> list = findAllAttrOption();
//        for(AttributeOption a:list){
//            Inventory inventory = new Inventory();
//            inventory.setCapacity(1000);
//            inventory.setRemains(1000);
//            addInventory(inventory);
//            a.setInventoryId(inventory.getId());
//            addAttributeOptions(a);
//            System.out.println("shezhiwanbi >>>>>>>>>>>> ");
//        }
////       /* Category category = new Category();
////        category.setCategory("手工布艺");
////        category.setDescription("以布为原料，集民间剪纸、刺绣、制作工艺为一体的综合艺术。如动植物身上的装饰性的花卉等，都是通过简和绣的工艺制作而成。这些生活日常用品不仅美观大方，而且增强了布料的强度和耐磨能力。");
////        category.setCategory_id("");
////        addCategory(category);*/
////
////        Attribute attribute = new Attribute();
////        attribute.setAttribute("塑料");
////        attribute.setCategory_id("6");
////        addAttribute(attribute);
////
////        AttributeOption attributeOption = new AttributeOption();
////        attributeOption.setFather_attr_id(attribute.getId());
////        attribute.setAttribute("花瓶");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("收纳");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("相框");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("生活用品");
////        addAttributeOptions(attributeOption);
////
////
////        Attribute attribute2 = new Attribute();
////        attribute2.setAttribute("编织");
////        attribute2.setCategory_id("6");
////        addAttribute(attribute2);
////
////        attributeOption.setFather_attr_id(attribute2.getId());
////        attribute.setAttribute("服饰");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("居家用品");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("首饰");
////        addAttributeOptions(attributeOption);
////
////        Attribute attribute3 = new Attribute();
////        attribute3.setAttribute("陶瓷");
////        attribute3.setCategory_id("6");
////        addAttribute(attribute3);
////
////        attributeOption.setFather_attr_id(attribute3.getId());
////        attribute.setAttribute("首饰");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("相框");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("动物");
////        addAttributeOptions(attributeOption);
////
////        Attribute attribute4 = new Attribute();
////        attribute4.setAttribute("手绘");
////        attribute4.setCategory_id("6");
////        addAttribute(attribute4);
////
////        attributeOption.setFather_attr_id(attribute4.getId());
////        attribute.setAttribute("装饰");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("花瓶");
////        addAttributeOptions(attributeOption);
////
////        attributeOption.setAttribute("儿童");
////        addAttributeOptions(attributeOption);
////
////
////        for(int i=1 ; i<=5 ; i++) {
////            ArrayList<byte[]> parames = new ArrayList<>();
////            parames.add(DataTable.CATEGORY_DETAIL_DATA.getBytes());
////            parames.add((i + "").getBytes());
////            curdRepository.hdel(parames);
////        }
////        System.out.println("。。。。delete success ");
////
////        for(int i=1 ; i<=5 ; i++) {
////            ArrayList<byte[]> parames = new ArrayList<>();
////            parames.add(DataTable.BRAND_DETAIL_DATA.getBytes());
////            parames.add((i + "").getBytes());
////            curdRepository.hdel(parames);
////        }
////        System.out.println("。。。。delete success ");
////
////
////        for(int i=1 ; i<=9 ; i++) {
////            ArrayList<byte[]> parames = new ArrayList<>();
////            parames.add(DataTable.ORIGIN_DETAIL_DATA.getBytes());
////            parames.add((i + "").getBytes());
////            curdRepository.hdel(parames);
////        }
////        System.out.println("。。。。delete success ");
////
////        for(int i=1 ; i<=5 ; i++) {
////            ArrayList<byte[]> parames = new ArrayList<>();
////            parames.add(DataTable.CITY_DETAIL_DATA.getBytes());
////            parames.add((i + "").getBytes());
////            curdRepository.hdel(parames);
////        }
////        System.out.println("。。。。delete success ");
////
////        for(int i=1 ; i<=5 ; i++) {
////            ArrayList<byte[]> parames = new ArrayList<>();
////            parames.add(DataTable.ATTRIBUTE_DETAIL_DATA.getBytes());
////            parames.add((i + "").getBytes());
////            curdRepository.hdel(parames);
////        }
////        System.out.println("。。。。delete success ");
////
////
//    }
}
